package com.example.baseadapter;

import java.util.Calendar;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Toast;

public class AddDate extends ActionBarActivity {
	DatabaseHelper db;
	Date date;
	public int caseId;
	public int year = 1999;
	public int month = 1999;
	public int day = 1999;
	EditText editDate;
	EditText editNotes;
	String notes;

	static final int DATE_PICKER_ID = 1111;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		db = new DatabaseHelper(getApplicationContext());
		date = new Date();
		// setting action bar background color
		getSupportActionBar().setBackgroundDrawable(
				new ColorDrawable(Color.rgb(10, 132, 132)));

		// this method will set the action bar app icon clickable.
		getSupportActionBar().setHomeButtonEnabled(true);
		// this method will set back button on app icon for the home screen.
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		setContentView(R.layout.add_date);
		caseId = getIntent().getExtras().getInt("caseId");
		editDate = (EditText) findViewById(R.id.edittext1);
		// to request focus on edittext
		editDate.requestFocus();
		editNotes = (EditText) findViewById(R.id.edittext2);

		// Get current date by calender
		final Calendar c = Calendar.getInstance();
		year = c.get(Calendar.YEAR);
		month = c.get(Calendar.MONTH);
		day = c.get(Calendar.DAY_OF_MONTH);

		// Button listener to show date picker dialog

		editDate.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				editDate.setText("");
				// On button click show datepicker dialog
				showDialog(DATE_PICKER_ID);

			}

		});

	}

	public void addDate(View v) {
		// String str=String.valueOf(idCase);
		// Toast.makeText(this,str,Toast.LENGTH_LONG).show();
		notes = editNotes.getText().toString();
		long id2 = db.createCaseDate(caseId, date, notes);
		Toast.makeText(this, "Your date has been saved successfully.",
				Toast.LENGTH_LONG).show();
		Log.e("Case Count", "Date count: " + db.getDateCount());

	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		// this case is used for back navigation click because CaseDetail class
		// want id of putExtra thats why we have to generate
		// intent
		case android.R.id.home:
			Intent h = new Intent(this, CaseDetail.class);
			h.putExtra("id", caseId);
			startActivity(h);
			return true;

			// Take appropriate action for each action item click

		default:
			return super.onOptionsItemSelected(item);
		}

	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DATE_PICKER_ID:

			// open datepicker dialog.
			// set date picker for current date
			// add pickerListener listner to date picker
			return new DatePickerDialog(this, pickerListener, year, month, day);
		}
		return null;
	}

	private DatePickerDialog.OnDateSetListener pickerListener = new DatePickerDialog.OnDateSetListener() {

		// when dialog box is closed, below method will be called.
		@Override
		public void onDateSet(DatePicker view, int selectedYear,
				int selectedMonth, int selectedDay) {

			year = selectedYear;
			month = selectedMonth + 1;
			day = selectedDay;

			date.setDay(day);
			date.setMonth(month);
			date.setYear(year);

			// Show selected date
			editDate.setText(new StringBuilder().append(day).append("-")
					.append(month).append("-").append(year).append(" "));

		}
	};
}
